#include "../mesh.h"
#include "3ds.h"
#include <sys/stat.h>

namespace sleek
{
    namespace mesh
    {
        long filelenght(int f)
        {
            struct stat buf;
            fstat(f, &buf);
            return buf.st_size;
        }
        MeshBuffer* LoadMeshFrom3ds(core::os::stringc file)
        {
            unsigned char lchar = 0;
            unsigned int lchunk_lenght = 0;
            unsigned short lchunk_id = 0, lqty = 0, lface_flags = 0;

            FILE *in = fopen(file.c_str(), "rb");
            if(!in){ printf("error: couldn't open \"%s\"!\n", file.c_str()); return 0; }
            printf("Load mesh: \"%s\"\n", file.c_str());

            MeshBuffer *tmp = new MeshBuffer();
            while(ftell(in) < 200)
            {
                fread(&lchunk_id, 2, 1, in);
                fread(&lchunk_lenght, 4, 1, in);

                switch (lchunk_id)
                {
                    case 0x4d4d: break;
                    case 0x3d3d: break;
                    case 0x4000: break;
                    case 0x4100: break;
                    case 0x4110:
                        fread(&lqty, sizeof (unsigned short), 1, in);
                        tmp->vertices.set_used(lqty);

                        for (core::u32 i = 0; i < lqty; i++)
                        {
                            fread(&tmp->vertices[i].Pos.x, sizeof(core::f32), 1, in);
                            fread(&tmp->vertices[i].Pos.y, sizeof(core::f32), 1, in);
                            fread(&tmp->vertices[i].Pos.z, sizeof(core::f32), 1, in);
                        }
                    break;
                    case 0x4120:
                        fread (&lqty, sizeof (unsigned short), 1, in);
                        tmp->indices.set_used(lqty);

                        for (core::u32 i = 0; i < lqty; i++)
                        {
                            fread (&tmp->indices[i].vertex[0], sizeof (unsigned short), 1, in);
                            fread (&tmp->indices[i].vertex[1], sizeof (unsigned short), 1, in);
                            fread (&tmp->indices[i].vertex[2], sizeof (unsigned short), 1, in);
                            fread (&lface_flags, sizeof (unsigned short), 1, in);
                        }
                    break;
                    case 0x4140:
                        fread (&lqty, sizeof (unsigned short), 1, in);
                        for (core::u32 i = 0; i < lqty; i++)
                        {
                            fread (&tmp->vertices[i].Coord.width,  sizeof (core::f32), 1, in);
                            fread (&tmp->vertices[i].Coord.height, sizeof (core::f32), 1, in);
                        }
                    break;
                    default:
                        fseek(in, lchunk_lenght - 6, SEEK_CUR);
                    break;
                }
            }
            tmp->updateNormal();
            return tmp;
        }
        bool WriteMeshTo3ds(MeshBuffer*,core::os::stringc)
        {
            return false;
        }
        bool IsMeshFormat3ds(core::os::stringc)
        {
            return false;
        }
    }
}
